package com.aaa.web.service.impl.employeeserviceimpl;

import com.aaa.entity.BackRole;
import com.aaa.entity.Per;
import com.aaa.web.commons.EmpQuery;
import com.aaa.web.commons.Result;
import com.aaa.web.dao.iemployeedao.IRoleDao;
import com.aaa.web.service.iemployeeservice.IRoleService;
import com.aaa.web.systemconst.ISystemConst;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao roleDao;

    /**
     * @Author yyh
     * @Description //获取所有角色信息
     * @Date 2018/12/26 9:47 
     * @Param [query]
     * @return java.util.Map
     **/
    @Override
    public Map getAllRole(EmpQuery query) {
        int page = query.getPage();
        int size = query.getLimit();
        PageHelper.startPage(page,size);
        Map m = new HashMap();
        m.put("data",roleDao.getAllRole(query));
        m.put("count",roleDao.getRoleCount());
        m.put("code",0);
        m.put("msg","");
        return m;
    }
    /**
     * @Author yyh
     * @Description //获取某个角色的信息
     * @Date 2018/12/26 9:49 
     * @Param [telephone]
     * @return java.util.Map
     **/
    @Override
    public Map getRoleById(String telephone) {
        return roleDao.getRoleById(telephone);
    }
    /**
     * @Author yyh
     * @Description //获取所有的权限信息以及当前角色拥有的权限
     * @Date 2018/12/26 9:50 
     * @Param [id]
     * @return java.util.List<com.aaa.entity.Per>
     **/
    @Override
    public List<Per> getRolePer(Integer id) {
        List<Per> mallp = roleDao.getAllParentPer();
        List<Per> mp =roleDao.getParentPer(id);
        List<Per> mc = roleDao.getChildPer(id);
        List<Per> mcc = roleDao.getChildCPer(id);
        for (int i = 0; i < mallp.size(); i++) {

            for (int j = 0; j < mp.size(); j++) {
                if(mallp.get(i).getValue().equals(mp.get(j).getValue())){
                    mallp.get(i).setChecked(true);
                }
            }

            List<Per> mallc=roleDao.getAllChildPer(mallp.get(i).getValue());
            for (int j = 0; j < mallc.size(); j++) {

                for (int k = 0; k < mc.size(); k++) {
                    if(mallc.get(j).getValue().equals(mc.get(k).getValue())){
                        mallc.get(j).setChecked(true);
                    }
                }
                List<Per> list= roleDao.getAllChildCPer(mallc.get(j).getValue());
                for (int k = 0; k < list.size(); k++) {
                    for (int l = 0; l < mcc.size(); l++) {
                        if(list.get(k).getValue().equals(mcc.get(l).getValue())){
                            list.get(k).setChecked(true);
                        }
                    }

                }
                mallc.get(j).setData(list);
                /*if(mallc.get(j).getValue().equals("11")||mallc.get(j).getValue().equals("44")||mallc.get(j).getValue().equals("5")){
                    mallc.remove(j);

                }*/
            }
            mallp.get(i).setData(mallc);
        }
        return mallp;
    }
    /**
     * @Author yyh
     * @Description //更新角色的的信息以及角色的权限
     * @Date 2018/12/26 9:51 
     * @Param [array, role]
     * @return com.aaa.web.commons.Result
     **/
    @Override
    public Result updatePer(int[] array, BackRole role) {
        int id = role.getId();
        int m = roleDao.deletePer(id);
        int u = roleDao.updatePer(role);
        Map map = new HashMap();
        Result result = new Result();
        map.put("roleid",id);
        if(array==null){

        }else{
            for (int j = 0; j < array.length; j++) {
                map.put("permissionid",array[j]);
                int n= roleDao.insertPer(map);
                if(n<1){
                    result.setCode(ISystemConst.DEFEAT_CODE);
                    return result;
                }
            }
        }

        int[] list = roleDao.getParRole(id);

        Map mp = new HashMap();
        mp.put("roleid",id);
        if(list==null){

        }else{
            for (int i = 0; i < list.length; i++) {

                mp.put("permissionid",list[i]);
                int k = roleDao.insertParRole(mp);
                if(k<1){
                    result.setCode(ISystemConst.DEFEAT_CODE);
                    return result;
                }
            }
        }
        /*int[] arr = roleDao.getParPRole(id);
        Map mapPar = new HashMap();
        mapPar.put("roleid",id);
        if(arr==null){

        }else{
            for (int i = 0; i < arr.length; i++) {

                mapPar.put("permissionid",arr[i]);
                int k = roleDao.insertParRole(mapPar);
                if(k<1){
                    result.setCode(ISystemConst.DEFEAT_CODE);
                    return result;
                }
            }
        }*/
        result.setCode(ISystemConst.SUCCESS_CODE);
        return result;

    }
    /**
     * @Author yyh
     * @Description //更新角色的状态
     * @Date 2018/12/26 9:52 
     * @Param [role]
     * @return com.aaa.web.commons.Result
     **/
    @Override
    public Result updateRoleState(BackRole role) {
        Integer flag =  roleDao.updateRoleState(role);
        Result result = new Result();
        if(flag>0){
            result.setCode(ISystemConst.SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.DEFEAT_CODE);
        }
        return result;
    }
    /**
     * @Author yyh
     * @Description //删除该角色，需要判断是否有员工还在使用该角色
     * @Date 2018/12/26 9:52 
     * @Param [id]
     * @return com.aaa.web.commons.Result
     **/
    @Override
    public Result deleteRole(Integer id) {
        Result result = new Result();
        if(roleDao.getRoleisExist(id)>0){
            result.setCode(ISystemConst.DEFEAT_CODE);
        }else{
            roleDao.deleteRole(id);
            roleDao.deleteRp(id);
            result.setCode(ISystemConst.SUCCESS_CODE);
        }

        return result;
    }
    /**
     * @Author yyh
     * @Description //添加角色
     * @Date 2018/12/26 9:52 
     * @Param [role]
     * @return com.aaa.web.commons.Result
     **/
    @Override
    public Result addRole(BackRole role) {
        Result result = new Result();
        Map map = roleDao.getRoleById(role.getName());
        if(map!=null){
            result.setCode(ISystemConst.DISABLE_CODE);
            return result;
        }
        Integer flag = roleDao.addRole(role);
        if(flag> 0){
            result.setCode(ISystemConst.SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.DEFEAT_CODE);
        }
        return result;
    }
}
